package com.opc.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author SEG
 */
public class DeviceCodeUtil {
    /**
     * 员工编号生成工具
     *
     * @return
     */
    public synchronized static String DeviceNumber() {
        //初始化
        String maxCode = "DCSEG20230321001";
        StringBuilder deviceCode = new StringBuilder();
        Integer codeLen = 16;
        //时间字符串产生方式
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        //组成流水号前的一部分
        String uidPfix = format.format(new Date());
        //判断数据库中是否有数据
        if (maxCode.contains(uidPfix)) {
            //从字符串FYHP202108080001截取后四位
            String uidEnd = maxCode.substring(12, 16);
            //将String类型的0001转换成int类型的1
            int endNum = Integer.parseInt(uidEnd);
            //将1变为1000x x=1+X
            int tmpNum = (int) (Math.random() * 1000);
            deviceCode = new StringBuilder("DCSEG" + uidPfix + tmpNum);
            if (deviceCode.length() < codeLen) {
                for (int i = codeLen - deviceCode.length(); i > 0; i--) {
                    deviceCode.append("0");
                }
            } else {
                deviceCode = new StringBuilder(deviceCode.substring(0, codeLen));
            }
        }
        return deviceCode.toString();
    }

//    public static void main(String[] args) {
//        for (int i = 0; i < 100; i++) {
//            String num = DeviceCodeUtil.DeviceNumber();
//            System.out.println(num);
//        }
//    }
}
